#!/usr/bin/perl -w
# Add ports to /etc/services
# vim:tw=100 sw=2 expandtab ft=perl

my @ports = flatten_list(c("$hostname/port_check"));
for (my $i = 0; $i < @ports; $i += 2) {
  my($name, $port, $comment) = ($ports[$i], $ports[$i + 1], undef);
  if (ref $port eq 'HASH') {
    $comment = $port->{comment};
    $port = $port->{port};
  }
  next unless $name && $port;
  next unless i_should($name) && i_should($port);

  $comment ||= "";
  $port = "$port/tcp" unless $port =~ /\//;

  file_append(-file => "/etc/services",
              -add => sprintf("%-15s %-20s # %s", $name, $port, $comment),
              -match => qr/^$name\s+$port/);

}
